package com.tianji.learning.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tianji.api.dto.leanring.LearningLessonDTO;
import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.common.exceptions.BadRequestException;
import com.tianji.common.utils.AssertUtils;
import com.tianji.common.utils.ObjectUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学生课程表 前端控制器
 * </p>
 *
 * @author ffk
 * @since 2024-06-20
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/lessons")
public class LearningLessonController {

    private final ILearningLessonService learningLessonService;

    @GetMapping("page")
    @ApiOperation("分页查询课程表")
    public PageDTO<LearningLessonVO> queryPage(PageQuery pageQuery){
        return learningLessonService.queryByPage(pageQuery);
    }

    @GetMapping("now")
    @ApiOperation("查询我正在学习的课程")
    public LearningLessonVO queryNowLesson(){
        return learningLessonService.queryNowLesson();
    }

    @ApiOperation("根据课程ID删除课程")
    @DeleteMapping ("/{courseId}")
    public void deleteLesson(@PathVariable("courseId") Long courseId)
    {
        learningLessonService.remove(new QueryWrapper<LearningLesson>().eq("course_id",courseId));
    }

    /**
     * 校验当前用户是否可以学习当前课程
     * @param courseId 课程id
     * @return true：课程有效，false：课程无效，不能学习
     */
    @ApiOperation("校验当前用户是否可以学习当前课程")
    @GetMapping("/{courseId}/valid")
    public Long isLessonValid(@PathVariable("courseId") Long courseId){
        return learningLessonService.isLessonValid(courseId);
    }

    /**
     * 统计课程学习人数
     * @param courseId 课程id
     * @return 学习人数
     */
    @ApiOperation("统计课程学习人数")
    @GetMapping("/{courseId}/count")
    public Integer countLearningLessonByCourse(@PathVariable("courseId") Long courseId){
        return learningLessonService.countLearningLessonByCourse(courseId);
    }



    @ApiOperation("查询用户课表中指定课程状态")
    @GetMapping("/{courseId}")
    public LearningLessonVO queryLesson(@PathVariable("courseId") Long courseId){
        return learningLessonService.queryLesson(courseId);
    }



    @ApiOperation("创建学习计划")
    @PostMapping("/plans")
    public void addPlanForLesson(@RequestParam(value = "courseId") Long courseId,@RequestParam(value = "weekFreq") Integer weekFreq){
         LearningLesson lessonMsg = learningLessonService.lambdaQuery().eq(LearningLesson::getCourseId, courseId)
                .eq(LearningLesson::getUserId, UserContext.getUser())
                .one();

         if(ObjectUtils.isEmpty(lessonMsg)){
             throw new BadRequestException("该课程不存在,无法创建计划");
         }

        LearningLesson lesson = new LearningLesson();
        lesson.setCourseId(courseId);
        lesson.setWeekFreq(weekFreq);
        learningLessonService.update(lesson,new QueryWrapper<LearningLesson>().eq("user_id",UserContext.getUser()));
    }

    @ApiOperation("分页查询我的学习计划")
    @GetMapping("/plans")
    public LearningPlanPageVO queryPlanPage(PageQuery pageQuery){
    return learningLessonService.queryPlanPage(pageQuery);
    }

}
